package cn.edu.scau.cmi.chenmiaopeng;
import java.util.Iterator;
import java.util.Scanner;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.chenmiaopeng.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.domain.Student;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.domain.Teacher;
public class HibernateClient_HibernateFactory {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		//插入学生：
//		insertStudent();
		//删除学生：
//		deleteStudent();
		//更新学生：
//		listStudents();
//		updateStudentId();
//		System.out.println();
//		listStudents();
		//按学号查询学生：
//		findStudentById();
		//按工号查询老师：
//		findTeacherById();
		//更改student id:
//		updateStudentId();
		//更改stu.name
//		listStudents();
//		updateStudentName();
//		System.out.println("更改之后的学生列表：");
//		listStudents();
		//插入老师：
//		insertTeacher();
		//删除老师：
//		deleteTeacher();
		//修改老师姓名：
//		updateTeacher();
		
		//给老师增加学生：
//		addStudentToTeacher();
		//给老师修改指导学生：
//		alterStudentToTeacher();
		//给老师删除指导学生：
//		deleteStudentToTeacher();
//		listTeachers();
		
		//给老师添加学生后，学生的导师栏也会同步改变
		//学生添加指导老师
//		addTeacherToStudent();
		//学生修改指导老师
		listStudents();
		alterTeacherToStudent();
		listStudents();
	}
	private static void addTeacherToStudent() {
		long teacherId,studentId;
		TeacherDAO teacherDAO = new TeacherDAO();
		StudentDAO studentDAO=new StudentDAO();
		System.out.println("请输入要增加导师的学生学号：");
		studentId = sc.nextLong();
		System.out.println("请输入要增加的导师工号：");
		teacherId = sc.nextLong();
		studentDAO.addTutor(teacherId, studentId);
		
	}
    private static void alterTeacherToStudent() {
    	long teacherId,studentId;
    	StudentDAO studentDAO = new StudentDAO();
    	System.out.println("请输入要修改导师的学生学号：");
    	studentId = sc.nextLong();
    	System.out.println("请输入要修改的导师工号：");
    	teacherId = sc.nextLong();
    	studentDAO.alterTutor(teacherId,studentId);
	}
	private static void addStudentToTeacher() {
		StudentDAO studentDAO=new StudentDAO();
		long studentId, teacherId;
		
		System.out.println("请输入要增加的学生学号：");
		studentId = sc.nextLong();
		Student stu = studentDAO.findById(studentId);
		
		TeacherDAO teacherDAO = new TeacherDAO();
		System.out.println("请输入要增加的老师工号：");
		teacherId = sc.nextLong();
		teacherDAO.addStudent(teacherId, stu);
	}
	//给老师修改指导学生
	private static void alterStudentToTeacher(){
		//调用teacherDAO修改老师的学生，将旧学生改成新学生
		long oldStuId, newStuId, teacherId;
		TeacherDAO teacherDAO = new TeacherDAO();
		System.out.println("请输入要修改的老师工号：");
		teacherId = sc.nextLong();
				
		StudentDAO studentDAO=new StudentDAO();
		
		System.out.println("请输入要给老师修改的旧学生学号：");
		oldStuId = sc.nextLong();
		Student oldStu = studentDAO.findById(oldStuId);
		System.out.println("请输入要给老师修改的新学生学号：");
		newStuId = sc.nextLong();
		Student newStu = studentDAO.findById(newStuId);
		
		teacherDAO.alterStudent(teacherId, oldStu, newStu);
	}
	//给老师删除指导学生
	private static void deleteStudentToTeacher(){
		//调用teacherDAO修改老师的学生，将旧学生改成新学生
		long deleteStuId, teacherId;
		TeacherDAO teacherDAO = new TeacherDAO();
		StudentDAO studentDAO = new StudentDAO();
		System.out.println("请输入要修改的老师工号：");
		teacherId = sc.nextLong();
		System.out.println("请输入要给老师删除的学生学号：");
		deleteStuId = sc.nextLong();
		Student deleteStu = studentDAO.findById(deleteStuId);
		teacherDAO.deleteStudent(teacherId,deleteStu);
	}
	private static void insertStudent() {
		Student stu = new Student(5L,"王欢");
		StudentDAO studentDAO=new StudentDAO();
		studentDAO.save(stu);
	}
	private static void deleteStudent() {
		Student stu = new Student(5L,"王欢");
		StudentDAO studentDAO=new StudentDAO();
		studentDAO.delete(stu);
	}
//	private static void updateStudentId() {  不成功
//		StudentDAO studentDAO=new StudentDAO();
//		studentDAO.updateId(4L,6L);  //不能这样做，会将创建一个新的student,其id为5
//	}
	private static void updateStudentName() {
		StudentDAO studentDAO=new StudentDAO();
//		studentDAO.updateName(4L, "刘华");
	}
	private static void findStudentById() {
		Long stu_id = 4L;
		StudentDAO studentDAO=new StudentDAO();
		Student stu = studentDAO.findById(stu_id);
		System.out.println("学生名是："+stu.getName()+"学号是:"+stu.getId());
	}
	private static void findTeacherById() {
		Long tea_id = 2L;
		TeacherDAO teacherDAO=new TeacherDAO();
		Teacher teacher = teacherDAO.findById(tea_id);
		System.out.println("老师名是："+teacher.getName()+"工号是:"+teacher.getId());
	}
	private static void listStudents() {
		StudentDAO studentDAO=new StudentDAO();
		List stus = studentDAO.findAll();
		Iterator<?> stuIterator = stus.iterator();
		while(stuIterator.hasNext()){
			Student stu = (Student) stuIterator.next();
			System.out.print("学生名是："+stu.getName()+" 学号是:"+stu.getId());  //+
			if(stu.getTeacher()==null) System.out.println("没有指导老师！");
			else System.out.println(" 指导老师是:"+stu.getTeacher().getId());
//            System.out.println("调用DAO后的学生专业是："+stu.getMajor().getId()+" "+stu.getMajor().getName());
	  }
	}
	private static void listTeachers() {
		TeacherDAO teacherDAO=new TeacherDAO();
		List teas = teacherDAO.findAll();
		Iterator<?> teaIterator = teas.iterator();
		while(teaIterator.hasNext()){
			Teacher teacher = (Teacher) teaIterator.next();
			System.out.println("老师名是："+teacher.getName()+"工号是:"+teacher.getId());  //+
			Set<Student> instr_stus = teacher.getStudents();
			if(instr_stus.size()==0) {
				 System.out.println("该老师没有指导学生。");
			}
			else {
				 System.out.println("该老师指导了"+instr_stus.size()+"学生。");
				 Iterator<Student> stu_itor = instr_stus.iterator();
				 while(stu_itor.hasNext()) {
					 Student stu = stu_itor.next();
					 System.out.println("--该老师指导的学生学号是："+stu.getId()+" 姓名是："+stu.getName());
				 }
				 System.out.println();
			}
            
	  }
	}
	private static void insertTeacher() {
		Teacher tea = new Teacher(4L,"陈华");
		TeacherDAO studentDAO=new TeacherDAO();
		studentDAO.save(tea);
	}
	private static void deleteTeacher() {
		//证明构造方法是delete的查询操作的依据
		Teacher tea = new Teacher(4L);   //创建老师对象传给session删除
		TeacherDAO studentDAO=new TeacherDAO();
		studentDAO.delete(tea);
	}
	private static void updateTeacher() {
		TeacherDAO studentDAO=new TeacherDAO();
		studentDAO.updateTeacherByName(2L, "王文");
	}
}
